<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\SliderModel;
use Illuminate\Http\Request;

class SliderController extends Controller
{

    public function sliders(Request $request)
    {
        $sliders=SliderModel::get();
        return view("admin.sliders" ,compact("sliders"));
    }


    public function addslider(Request $request)
    {
        return view("admin.addslider");
    }



    public function insertslider(Request $request)
    {
        $id=$request->input("id");
        $sliders=SliderModel::find($id);
        $input = $request->input();
        $slider = $request->file('slider');
//       dd($input,$slider);
        $slider_name = "sliderimg_" . date("ymd_his", time()) . rand(1, 1000) . "." . $slider->getClientOriginalExtension();
        $slider->move(public_path('slider_image'), $slider_name);
        $input['slider'] = "slider_image/" . $slider_name;
        if ($id==0){
            $ok = SliderModel::create($input);
            if ($ok) {
                return $this->success(null, [
                    "title" => "操作成功",
                    "text" => "数据添加成功",
                ]);
            }
        }else{
            $ok = $sliders->update($input);
            if ($ok) {
                return $this->success(null, [
                    "title" => "操作成功",
                    "text" => "数据修改成功",
                ]);
            }
        }
    }



    public function updateslider(Request $request)
    {
        $id=$request->input("id");

        $sliders=SliderModel::find($id);
        return view("admin.addslider",compact("sliders"));
    }

    public function deleteslider(Request $request)
    {
        $id=$request->input("id");
        $ok=SliderModel::find($id)->delete();
        if ($ok) {
            return $this->success(null, [
                "title" => "操作成功",
                "text" => "数据修改成功",
            ]);
        }
    }
}
